package com.zzwtec.permission.controller;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.zzwtec.permission.model.NewsInfo;
import com.zzwtec.permission.dao.NewsInfoDao;
import com.zzwtec.interfaces.ResultObject;
import com.zzwtec.common.exception.BizException;
import com.zzwtec.common.util.BeanUtil;
import com.zzwtec.common.util.ErrorMsg;
import com.zzwtec.interfaces.Page;
import com.zzwtec.interfaces.RequestObject;
import com.zzwtec.interfaces.permission.dto.NewsInfoDTO;
import com.zzwtec.interfaces.permission.services.NewsInfoService;
/**
 * 微服务NewsInfoService接口实现类
 * @author  草原狼
 * 
 * 2017年08月02日
 */
@RestController
public class NewsInfoController implements NewsInfoService {
	
	@Autowired
	private DiscoveryClient discoveryClient;

	@Resource(name = "newsInfoDTO")
	private NewsInfoDao newsInfoDao;

	@Override
	public ResultObject<String> addNewsInfo(@RequestBody RequestObject<NewsInfoDTO> request) {
		ResultObject<String> result = new ResultObject<String>(request);
		NewsInfoDTO dto = request.getData();
		NewsInfo bean = new NewsInfo();
		BeanUtil.copyProperties(bean, dto);		
		newsInfoDTO.insert(bean);
		result.setData(bean.getId());
		return result;
		
	}
	
	@Override
	public ResultObject<Void> removeNewsInfo(@RequestBody RequestObject<String> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		newsInfoDao.deleteById(request.getData());
		return result;	
		
	}


	@Override
	public ResultObject<Void> modifyNewsInfo(@RequestBody RequestObject<NewsInfoDTO> request) {
		ResultObject<Void> result = new ResultObject<Void>(request);
		NewsInfoDTO dto = request.getData();
		NewsInfo bean = new NewsInfo();
		BeanUtil.copyProperties(bean, dto);		
		newsInfoDao.update(bean);
		return result;
		
	}


	@Override
	public ResultObject<List<NewsInfoDTO>> queryAllNewsInfo(@RequestBody RequestObject<Void> request) {
		ResultObject<List<NewsInfoDTO>> result = new ResultObject<List<NewsInfoDTO>>(request);
		List<NewsInfo> list = newsInfoDao.selectAll();
		List<NewsInfoDTO> data = new ArrayList<NewsInfoDTO>();
		for(NewsInfo entity : list){
			NewsInfoDTO dto = new NewsInfoDTO();
			BeanUtil.copyProperties(dto, entity);
			data.add(dto);
		}
		result.setData(data);
		return result;	
		
	}


	@Override
	public ResultObject<NewsInfoDTO> queryNewsInfoById(@RequestBody RequestObject<String> request) {
		ResultObject<NewsInfoDTO> result = new ResultObject<NewsInfoDTO>(request);
		NewsInfo entity = newsInfoDao.selectOne(request.getData());
		NewsInfoDTO dto = new NewsInfoDTO();
		BeanUtil.copyProperties(dto, entity);
		result.setData(dto);
		return result;	
		
	}

	@Override
	public ResultObject<Page<NewsInfoDTO>> queryNewsInfoByPage(@RequestBody RequestObject<Page<NewsInfoDTO>> request) {
		ResultObject<Page<NewsInfoDTO>>  result=new ResultObject<Page<NewsInfoDTO>>(request);
		Page<NewsInfoDTO> page=request.getData();
		Page<NewsInfo> rPage=new Page<NewsInfo>();
		rPage.setCondition(page.getCondition());
		rPage.setDistinct(page.isDistinct());
		rPage.setGroupByFields(page.getGroupByFields());
		rPage.setOrderField(page.getOrderField());
		rPage.setOrderType(page.getOrderType());
		rPage.setPage(page.getPage());
		rPage.setPageNum(page.getPageNum());
		rPage.setSearchFields(page.getSearchFields());
		rPage.setSearchValues(page.getSearchValues());
		rPage = newsInfoDao.selectPage(rPage);
		if(rPage.getData()!=null){
			List<NewsInfoDTO> data = new ArrayList<NewsInfoDTO>(rPage.getData().size());
			for(NewsInfo entity : rPage.getData()){	
				NewsInfoDTO dto = new NewsInfoDTO();
				BeanUtil.copyProperties(dto, entity);
				data.add(dto);
			}
			page.setData(data);
		}
		page.setTotal(rPage.getTotal());
		page.setRows(rPage.getRows());
		result.setData(page);
		result.setCode("0");
		result.setMsg(ErrorMsg._0);
		return result;
	}
	
	
	@Override
	public ServiceInstance showServiceInfo() {		
		ServiceInstance localServiceInstance = this.discoveryClient.getLocalServiceInstance();
		return localServiceInstance;
	}
}
